// pages/login/index.js
var utl = require('../../utils/util.js');
var mdk = require('../../utils/md5.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ischecked: true,
    telephone:'',
    serct:'',
    cantouch:false,
    validcode: '',  //验证码
    verification: '',
    showtime: '重新发送(120s)',
    showverification:true,
    shownote:'获取验证码'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({ 
      key: 'USERNAME',
      success: function(res) {
        console.log('用户名'+res.data);
        var val = res.data;
        that.setData({ 
          telephone: val
        })
      },
    })
    wx.getStorage({
      key: 'PASSW',
      success: function(res) {
        that.setData({
          serct: res.data,
          cantouch:true
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var ischeck = this.data.ischecked;
    ischeck = !ischeck;
    this.setData({
      ischecked: ischeck
    })
  },
  //验证功能
  loginaction:function(){
    const tele = this.data.telephone;
    const secrt = this.data.serct;
    const ischeck = this.data.ischecked;
    const mcode = this.data.messcode;
    if (tele == '' && secrt==''&& mcode==''){
      return;
    }
    var secr = mdk.hexMD5(secrt);
    wx.showLoading({
      title: '验证中...',
    })
    var session = app.globalData.sessonid;
    var uid = app.globalData.userid;
    utl.httServer('USER_BIND_PHONE', { 'USER_ID': uid, 'SESSION_ID': session, 'MOBILE': tele, 'PASSWORD': secr, 'CODE': mcode }, (err, res) => {
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000,
        })
      } else {
        app.globalData.telephone = tele;
        wx.navigateTo({
          url: '/pages/login/uinfo',
        })
      }
    })
    // utl.httServer('/epay_apps/login/loginA0100', { 'loginName': tele, 'passWord': secr,"clientId":'' }, (err, res) => {
    //   wx.hideLoading();
    //   if(err){
    //     wx.showToast({
    //       title: err,
    //       icon:'none',
    //       duration:2000,
    //     })
    //     return;
    //   }
    //   app.globalData.token = res.response.auth_token;
    //   app.globalData.userData = res.response.userInfo;
    //   if (ischeck){
    //     wx.setStorage({
    //       key: 'USERNAME',
    //       data: tele,
    //     })
    //     wx.setStorage({
    //       key: 'PASSW',
    //       data: secrt,
    //     })
    //   }else{
    //     wx.setStorage({
    //       key: 'USERNAME',
    //       data: '',
    //     })
    //     wx.setStorage({
    //       key: 'PASSW',
    //       data: '',
    //     })
    //   }
    //   wx.reLaunch({
    //     url: '/pages/index/index?section=' + "1",
    //     // url:'/pages/regist/userredit'
    //   })
    // })
  
  },
  //获取验证码
  yanzhenm: function () {
    var tel = this.data.telephone;
    if(tel == '' ){
      wx.showToast({
        title: '请输入手机号',
        icon:'none',
        duration:2000
      })
      return;
    }
    var session = app.globalData.sessonid;
    var uid = app.globalData.userid;
    utl.httServer('USER_VERIFY', { 'MOBILE': tel, 'USER_ID': uid, 'SEESION_ID': session, 'CARD_NO': '','TYPE':'3' }, (err, res) => {
      if(err){
        wx.showToast({
          title: err,
          icon:'none',
          duration:2000,
        })
      }else{
        wx.showToast({
          title: '发送成功',
          icon:'none',
          duration:2000
        })
      }
    })
    this.beginTimer();
  },
  // 启动倒计时
  beginTimer() {
    var Timers = null;
    this.setData({
      showt: true,
      showverification: false,
      count: 120,
      Timers: null,
      showtime: '重新发送(120s)'
    })
    var that = this;
    //如果定时器已经器动了
    if (!Timers) {
      Timers = setInterval(function () {
        var count = that.data.count - 1;
        that.setData({
          showtime: "重新发送(" + count + "s)",
          Timers: Timers,
          count: count
        })
        if (count <= 0) {
          clearInterval(Timers)
          that.setData({
            count: 120,
            showt: false,
            showverification: true,
            shownote:'重新发送'
          })
        }
      }, 1000);
    }
  },
  //手机号
  telephoneAction:function(e){
    this.setData({
      telephone:e.detail.value,
    })
  },
  //验证码输入
  messcodeRecive:function(e){
    this.setData({
      messcode:e.detail.value
    })
  },
  //密码
  secretAction:function(e){
    this.setData({
      serct:e.detail.value,
      cantouch:true,
    })
  }
  
})